"""
家庭成员数据库模型
"""

import enum
from datetime import datetime

from sqlalchemy import Boolean, DateTime, Enum, Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from src.database import Base


class FamilyRole(enum.Enum):
    """家庭成员角色枚举"""

    ADMIN = "admin"  # 管理员(家庭负责人)
    MEMBER = "member"  # 普通成员
    CHILD = "child"  # 子女
    GUEST = "guest"  # 访客


class FamilyRelationship(enum.Enum):
    """家庭关系枚举"""

    FATHER = "father"  # 父亲
    MOTHER = "mother"  # 母亲
    SON = "son"  # 儿子
    DAUGHTER = "daughter"  # 女儿
    GRANDFATHER = "grandfather"  # 爷爷
    GRANDMOTHER = "grandmother"  # 奶奶
    OTHER = "other"  # 其他


class Family(Base):
    """家庭模型"""

    __tablename__ = "family"

    name: Mapped[str] = mapped_column(String(50), unique=True, nullable=False)


class FamilyMember(Base):
    """家庭成员模型"""

    __tablename__ = "family_members"

    username: Mapped[str] = mapped_column(
        String(50), unique=True, index=True, nullable=False
    )
    email: Mapped[str] = mapped_column(
        String(100), unique=True, index=True, nullable=False
    )
    hashed_password: Mapped[str] = mapped_column(String(255), nullable=False)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)

    # 家庭相关信息
    full_name: Mapped[str] = mapped_column(
        String(100), nullable=False, comment="真实姓名"
    )
    nickname: Mapped[str | None] = mapped_column(
        String(50), nullable=True, comment="昵称"
    )
    role: Mapped[FamilyRole] = mapped_column(
        Enum(FamilyRole), default=FamilyRole.MEMBER, nullable=False, comment="家庭角色"
    )
    relationship: Mapped[FamilyRelationship | None] = mapped_column(
        Enum(FamilyRelationship), nullable=True, comment="家庭关系"
    )
    family_id: Mapped[int] = mapped_column(
        Integer, nullable=False, comment="家庭ID,用于区分不同家庭"
    )
    family_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="家庭名称"
    )

    # 个人信息
    phone: Mapped[str | None] = mapped_column(
        String(20), nullable=True, comment="手机号码"
    )
    avatar: Mapped[str | None] = mapped_column(
        String(255), nullable=True, comment="头像URL"
    )
    birth_date: Mapped[datetime | None] = mapped_column(
        DateTime, nullable=True, comment="出生日期"
    )
    gender: Mapped[str | None] = mapped_column(
        String(10), nullable=True, comment="性别"
    )

    def __repr__(self):
        return f"<FamilyMember(id={self.id}, username='{self.username}', full_name='{self.full_name}', role='{self.role.value}')>"
